﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI.WebControls;
using System.Data;
using MyLib;

namespace BudgetSet
{
    public partial class ModifyBudget : System.Web.UI.Page
    {

        static string strID ="";

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                //获取参数ID
                strID = Request.QueryString["ID"].ToString();
                //查询待修改记录
                string Sql = string.Format("select * from PROC_BUDGETSET where ID='{0}'", strID);
                DataTable dt = DataAccess.Instance("BizDB").ExecuteDataTable(Sql);
                if (dt.Rows.Count == 1)
                {
                    if (dt.Rows[0]["BUDGETTYPE"].ToString() == "E")
                    {
                        RadioExpense.Checked = true;
                    }
                    else
                    {
                        RadioCapex.Checked = true;
                    }

                    hf_BudgetType.Value = dt.Rows[0]["BUDGETTYPE"].ToString();

                    fld_STARTTIME.Text = dt.Rows[0]["STARTTIME"].ToString();
                    fld_ENDTIME.Text = dt.Rows[0]["ENDTIME"].ToString();
                    fld_BUDGETCODE.Text = dt.Rows[0]["BUDGETCODE"].ToString();
                    fld_DEPARTMENT.Text = dt.Rows[0]["DEPARTMENT"].ToString();
                    fld_ITEMSCODE.Text = dt.Rows[0]["ITEMSCODE"].ToString();
                    fld_ITEMSDESC.Text = dt.Rows[0]["ITEMSDESC"].ToString();
                    fld_COSTCENTER.Text = dt.Rows[0]["COSTCENTER"].ToString();
                    fld_COSTCENTERNAME.Text = dt.Rows[0]["COSTCENTERNAME"].ToString();
                    fld_COSTCATEGORY1.Text = dt.Rows[0]["COSTCATEGORY1"].ToString();
                    fld_COSTCATEGORY2.Text = dt.Rows[0]["COSTCATEGORY2"].ToString();
                    fld_ACCOUNTCODE.Text = dt.Rows[0]["ACCOUNTCODE"].ToString();
                    fld_ACCOUNTDESC.Text = dt.Rows[0]["ACCOUNTDESC"].ToString();
                    fld_DETAILACCOUNTCODE.Text = dt.Rows[0]["DETAILACCOUNTCODE"].ToString();
                    fld_DETAILACCOUNTDESC.Text = dt.Rows[0]["DETAILACCOUNTDESC"].ToString();
                    fld_REASONS.Text = dt.Rows[0]["REASONS"].ToString();
                    fld_DESCRIPTION.Text = dt.Rows[0]["DESCRIPTION"].ToString();
                    fld_OWNER.Text = dt.Rows[0]["OWNER"].ToString();
                    fld_LIFE.Text = dt.Rows[0]["LIFE"].ToString();
                    fld_QTY.Text = dt.Rows[0]["QTY"].ToString();
                    fld_AMOUNT.Text = dt.Rows[0]["AMOUNT"].ToString();
                    fld_CAPEXBALANCE.Text = dt.Rows[0]["CAPEXBALANCE"].ToString();
                    fld_PRBALANCE.Text = dt.Rows[0]["PRBALANCE"].ToString();
                    fld_POBALANCE.Text = dt.Rows[0]["POBALANCE"].ToString();
                    fld_DH.Text = dt.Rows[0]["DH"].ToString();
                    fld_RECLASS.Text = dt.Rows[0]["RECLASS"].ToString();
                }
            }
        }

        /// <summary>
        /// 保存数据至数据库
        /// </summary>
        /// <param name="Value"></param>
        /// <returns></returns>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (CheckData())
                {
                    string Sql = GetUpdateSQL();
                    DataAccess.Instance("BizDB").ExecuteDataTable(Sql);
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('修改成功！');", true);
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "closeWin", "<script>closeWin();</script>");
                }
            }
            catch (Exception ee)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('新增失败：" + ee.Message + "');", true);
            }
            finally
            {
            }
        }

        /// <summary>
        /// 数据判断
        /// </summary>
        protected Boolean CheckData()
        {
            if (fld_STARTTIME.Text == "")
            {
                Response.Write("<script>alert('预算开始时间不能为空！')</script> ");
                return false;
            }
            else if (fld_ENDTIME.Text == "")
            {
                Response.Write("<script>alert('预算结束时间不能为空！')</script> ");
                return false;
            }
            else if (hf_BudgetType.Value == "")
            {
                Response.Write("<script>alert('预算类型不能为空！')</script> ");
                return false;
            }
            else if (fld_BUDGETCODE.Text == "")
            {
                Response.Write("<script>alert('预算编号不能为空！')</script> ");
                return false;
            }
            else if (fld_DEPARTMENT.Text == "")
            {
                Response.Write("<script>alert('部门不能为空！')</script> ");
                return false;
            }
            else if (fld_ITEMSCODE.Text == "")
            {
                Response.Write("<script>alert('项目代码不能为空！')</script> ");
                return false;
            }
            else if (fld_ITEMSDESC.Text == "")
            {
                Response.Write("<script>alert('项目描述不能为空！')</script> ");
                return false;
            }
            else if (fld_COSTCENTER.Text == "")
            {
                Response.Write("<script>alert('成本中心不能为空！')</script> ");
                return false;
            }
            else if (fld_COSTCENTERNAME.Text == "")
            {
                Response.Write("<script>alert('成本中心名称不能为空！')</script> ");
                return false;
            }
            else if (fld_AMOUNT.Text == "" || fld_AMOUNT.Text == "0")
            {
                Response.Write("<script>alert('预算金额不能为0！')</script> ");
                return false;
            }
            else if (fld_BUDGETCODE.Text != "")
            {
                //当BudgetCode与数据库里存在时，对用户进行提示
                string SqlRecord = string.Format("select * from PROC_BUDGETSET where BUDGETCODE ='{0}' and ID <> '{1}'", fld_BUDGETCODE.Text, strID);
                DataTable dtRecord = DataAccess.Instance("BizDB").ExecuteDataTable(SqlRecord);
                if (dtRecord.Rows.Count > 0)
                {
                    Response.Write("<script>alert('您不能将预算编号修改为：" + fld_BUDGETCODE.Text + "，因为该编号的记录已经存在！')</script> ");
                    return false;
                }
                else return true;
            }
            else
                return true;
        }

        /// <summary>
        /// 拼接UpdateSQL
        /// </summary>
        protected string GetUpdateSQL()
        {
            string StrValues = "DEPARTMENT = '" + fld_DEPARTMENT.Text
                             + "' ,STARTTIME = '" + fld_STARTTIME.Text
                             + "' ,ENDTIME = '" + fld_ENDTIME.Text
                             + "' ,BUDGETTYPE = '" + hf_BudgetType.Value
                             + "' ,BUDGETCODE = '" + fld_BUDGETCODE.Text
                             + "' ,ITEMSCODE = '" + fld_ITEMSCODE.Text
                             + "' ,ITEMSDESC = '" + fld_ITEMSDESC.Text
                             + "' ,COSTCENTER = '" + fld_COSTCENTER.Text
                             + "' ,COSTCENTERNAME = '" + fld_COSTCENTERNAME.Text
                             + "' ,COSTCATEGORY1 = '" + fld_COSTCATEGORY1.Text
                             + "' ,COSTCATEGORY2 = '" + fld_COSTCATEGORY2.Text
                             + "' ,ACCOUNTCODE = '" + fld_ACCOUNTCODE.Text
                             + "' ,ACCOUNTDESC = '" + fld_ACCOUNTDESC.Text
                             + "' ,DETAILACCOUNTCODE = '" + fld_DETAILACCOUNTCODE.Text
                             + "' ,DETAILACCOUNTDESC = '" + fld_DETAILACCOUNTDESC.Text
                             + "' ,REASONS = '" + fld_REASONS.Text
                             + "' ,DESCRIPTION = '" + fld_DESCRIPTION.Text
                             + "' ,OWNER = '" + fld_OWNER.Text
                             + "' ,LIFE = " + fld_LIFE.Text
                             + " ,QTY = " + fld_QTY.Text
                             + " ,AMOUNT = " + fld_AMOUNT.Text
                            // + " ,CAPEXBALANCE = " + fld_CAPEXBALANCE.Text
                            // + " ,PRBALANCE = " + fld_PRBALANCE.Text
                            // + " ,POBALANCE = " + fld_POBALANCE.Text
                             + " ,DH = '" + fld_DH.Text
                             + "' ,RECLASS = '" + fld_RECLASS.Text
                             + "'";
            string Sql = string.Format("update PROC_BUDGETSET set {0} where ID = '{1}'", StrValues, strID);
            strID = Request.QueryString["ID"].ToString();
            return Sql;
        }
    }
}